package com.liuhaozzu.messageq.rabbitmq;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

public class ResvVhost {
	private static final String QUEUE_NAME="quname";
	public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
		ConnectionFactory factory=new ConnectionFactory();
		factory.setHost("hadoop1");
		factory.setUsername("liuhaozzu");
		factory.setVirtualHost("test");
		factory.setPassword("liuhao1989");
		factory.setPort(5672);
		Connection conn=factory.newConnection();
		Channel channel=conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		System.out.println("waiting for messages...");
		QueueingConsumer consumer=new QueueingConsumer(channel);
		channel.basicConsume(QUEUE_NAME, true,consumer);
		
		while(true){
			QueueingConsumer.Delivery delivery=consumer.nextDelivery();
			String message=new String(delivery.getBody());
			System.out.println("received:'"+message+"'");
		}
	}
}
